package legoproject.TestStubs;

import java.net.*;
import java.io.*;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import legoproject.*;

public class BluetoothManagerTestStub {
    private Socket socket;
    private DataOutputStream os;
    private DataInputStream is;
    private JTextArea component;
    private PCReporter reporter;

    public BluetoothManagerTestStub(PCReporter reporter){
        this.reporter = reporter;
    }

    public void init() {
        try{
        socket = new Socket("localhost", 12345);
        is = new DataInputStream(socket.getInputStream());
        os = new DataOutputStream(socket.getOutputStream());
        }catch(Exception e){
            System.out.println(e.toString());
        }

        component = new JTextArea("BluetoothManager\n", 2, 25);
        component.setOpaque(true);
        reporter.addComponent(component);

    }

    public int sendCommand(int command) {

        int result = -1;
        try{
        socket = new Socket("localhost", 12345);
        os.writeInt(command);
        os.flush();
        result = is.readInt();
        String commandName;
        if(command == CommandType.GRAB){
            commandName = "GRAB";
        }
        else if(command == CommandType.READTOUCH){
            commandName = "READLIGHT";
        }
        else if(command == CommandType.FINDHOME){
            commandName = "READULTRASONIC";
        }
        else if(command == CommandType.RELEASEGRAB){
            commandName = "RELEASEGRAB";
        }
        else if(command == CommandType.SHUTDOWN){
            commandName = "SHUTDOWN";
        }
        else{
            commandName = "unknown command!";
        }
        component.append(commandName + " returned: " +result + ", ");
        }catch(Exception e){
        System.out.println(e.toString());

        }
        return result;
    }
}
